package com.mall.user.services;

import com.alibaba.fastjson.JSON;
import com.mall.user.ILoginService;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.converter.UserConverterMapper;
import com.mall.user.dal.entitys.Member;
import com.mall.user.dal.entitys.UserVerify;
import com.mall.user.dal.persistence.MemberMapper;
import com.mall.user.dal.persistence.UserVerifyMapper;
import com.mall.user.dto.CheckAuthRequest;
import com.mall.user.dto.CheckAuthResponse;
import com.mall.user.dto.UserLoginRequest;
import com.mall.user.dto.UserLoginResponse;
import com.mall.user.utils.JwtTokenUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.DigestUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.HashMap;

/**
 * @Description:
 * @author: Jason 805636901@qq.com
 * @create: 2021-11-02-2:30 PM
 */
@Service
@Component
@Slf4j
public class UserLoginServiceImpl implements ILoginService {

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private UserVerifyMapper userVerifyMapper;

    @Autowired
    private UserConverterMapper userConverterMapper;

    @Override
    public UserLoginResponse userLogin(UserLoginRequest request) {
        UserLoginResponse response = new UserLoginResponse();
        request.requestCheck();
        Example example = new Example(Member.class);
        example.createCriteria().andEqualTo("username",request.getUserName()).andEqualTo("password", DigestUtils.md5DigestAsHex(request.getPassword().getBytes()));
        Member member = memberMapper.selectOneByExample(example);
        if (member == null){
            response.setCode(SysRetCodeConstants.USERORPASSWORD_ERRROR.getCode());
            response.setMsg(SysRetCodeConstants.USERORPASSWORD_ERRROR.getMessage());
            return response;
        }
        //判断用户是否激活
        //Example userVerifyExample = new Example(UserVerify.class);
        //userVerifyExample.createCriteria().andEqualTo("username",request.getUserName());
        //
        //UserVerify userVerify = userVerifyMapper.selectOneByExample(userVerifyExample);
        //if ("N".equals(userVerify.getIsVerify())){
        //    response.setCode(SysRetCodeConstants.USER_ISVERFIED_ERROR.getCode());
        //    response.setMsg(SysRetCodeConstants.USER_ISVERFIED_ERROR.getMessage());
        //    return response;
        //}
        //判断用户是否激活
        if (member.getIsVerified().equals("N")) {
            response.setCode(SysRetCodeConstants.USER_ISVERFIED_ERROR.getCode());
            response.setMsg(SysRetCodeConstants.USER_ISVERFIED_ERROR.getMessage());
        }



        //走到这说明用户已经激活了
        //产生JWT放入到token中
        HashMap<String, Object> map = new HashMap<>();
        map.put("uid",member.getId());
        map.put("file",member.getFile());
        map.put("username",member.getUsername());
        String token = JwtTokenUtils.builder().msg(JSON.toJSONString(map)).build().creatJwtToken();
        response = userConverterMapper.converter(member);
        response.setToken(token);
        response.setCode(SysRetCodeConstants.SUCCESS.getCode());
        response.setMsg(SysRetCodeConstants.SUCCESS.getMessage());
        return response;
    }

    @Override
    public CheckAuthResponse validToken(CheckAuthRequest request) {
        CheckAuthResponse response = new CheckAuthResponse();
        try {
            request.requestCheck();
            String info = JwtTokenUtils.builder().token(request.getToken()).build().freeJwt();
            response.setUserinfo(info);
            response.setCode(SysRetCodeConstants.SUCCESS.getCode());
            response.setMsg(SysRetCodeConstants.SUCCESS.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            response.setCode(SysRetCodeConstants.TOKEN_VALID_FAILED.getCode());
            response.setMsg(SysRetCodeConstants.TOKEN_VALID_FAILED.getMessage());
        }
        return response;
    }
}
